package com.wx.exam.controller.teacher;

import com.wx.exam.pojo.StatisticsData;
import com.wx.exam.pojo.Teacher;
import com.wx.exam.pojo.data.TeacherDO;
import com.wx.exam.pojo.vo.ExamVO;
import com.wx.exam.pojo.vo.QuestionManageVO;
import com.wx.exam.pojo.vo.StudentReportVO;
import com.wx.exam.service.ExamService;
import com.wx.exam.service.ExaminationResultService;
import com.wx.exam.service.QuestionService;
import com.wx.exam.utils.DataUtils;
import com.wx.exam.utils.ExamUtils;
import com.wx.exam.utils.PageBean;
import com.wx.exam.utils.Result;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.commons.io.FileUtils;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/teacher/exam")
public class TeacherExamController {

    int pageSize=5;
    int size=5;

    @Autowired
    ExamService examService;

    @Autowired
    QuestionService questionService;

    @Autowired
    ExaminationResultService examinationResultService;

    @RequestMapping("/list")
    public String list(ExamVO examVO,Model model) throws Exception {
        model.addAttribute("function",2);
        examVO.setPageSize(pageSize);
        examVO.setSize(size);
        PageBean<ExamVO> pageBean = examService.listExam(examVO);
        model.addAttribute("pageBean",pageBean);
        return "teacher/exam_list";
    }

    //添加新的考试
    @RequestMapping("/add")
    @ResponseBody
    public Result add(ExamVO examVO, HttpServletRequest request) throws Exception {
        examVO.setEndtime(new Date());//先随便设置
        examVO.setFkStatus(1);
        TeacherDO teacher = (TeacherDO)request.getSession().getAttribute("teacher");
        examVO.setFkTeacher(teacher.getId());
        examVO.setPoints(0);
        examVO.setSinglepoints(0);
        examVO.setMultipoints(0);
        examVO.setJudgepoints(0);
        return examService.addExam(examVO);
    }

    /**
     * 获取试卷列表
     * @param eid
     * @param pageCode
     * @param model
     */
    @RequestMapping("/examManager/{eid}")
    public String examManager(@PathVariable Integer eid,Integer pageCode,Model model){

        pageCode=DataUtils.getPageCode(pageCode+"");
       PageBean<QuestionManageVO> pageBean=
               questionService.listQuestionByExam(eid,pageCode,pageSize,size);
       model.addAttribute("pageBean",pageBean);
       model.addAttribute("examId",eid);
       return "teacher/question_manage";
    }


    @RequestMapping("/get/{eid}")
    @ResponseBody
    public Result get(@PathVariable Integer eid) throws Exception {
       return examService.findDetailExam(ExamVO.getClassByEXAM(eid));
    }

    /**
     * 切换考试状态
     *
     * teacher/exam/status?examId=1&status=RUNNING&days=2
     */
    @RequestMapping("/status")
    @ResponseBody
    public Result status(Integer examId,String status,Integer days){
        ExamVO examVO=new ExamVO();
        examVO.setId(examId);

        //有两种情况 1.开始运行 和重新运行是一样的  2.结束运行
        if(ExamUtils.RUNNING_EN.equals(status)){
            Calendar calendar=Calendar.getInstance();
            calendar.add(Calendar.DATE,days);
            examVO.setEndtime(calendar.getTime());
        }else{
            examVO.setEndtime(new Date());
        }
        examVO.setFkStatus(ExamUtils.getStatusNumByEN(status));
       return examService.updateExamStatus(examVO);
    }

    @RequestMapping("/statistics/{eid}")
    public String toStatistics(@PathVariable Integer eid ,Model model){
        return "teacher/statistics";
    }

    @RequestMapping("/statistics/do/{eid}")
    @ResponseBody
    public Result statistics(@PathVariable Integer eid ,Model model,HttpServletRequest request) throws IOException {
      StatisticsData data= examinationResultService.getStatistics(eid);
      if(data==null){
          return Result.getFailure("没有统计记录");
      }
        String path = request.getServletContext().getRealPath("/") + "/";
//      String path=this.getClass().getClassLoader().getResource("/").getPath();
        System.out.println(path);
        //String path="D:\\wxiao\\ExamSystem\\web\\src\\main\\webapp\\";
        checkPath(path + "charts");
        String imagePath = "charts/pie_" + eid + ".png";
        createCharts(path + imagePath, data);
        data.setUrl(imagePath);
        return new Result(Result.CODE_SUCCESS, data);
    }

    @RequestMapping("/download/{eid}")
    public String download(@PathVariable Integer eid,Model model){
        model.addAttribute("eid",eid);
        return "teacher/download";
    }


    /**
     * 下载成绩表
     * @param eid
     */
    @RequestMapping("/report/{eid}")
    public ResponseEntity<byte[]> report(@PathVariable Integer eid,HttpServletRequest request) throws Exception {
        String path = request.getServletContext().getRealPath("/") + "/";
        checkPath(path + "report");
        String fileName="/report/report_"+eid+".xls";
       List<StudentReportVO> reportVOList= examinationResultService.getReportData(eid);
        excel(reportVOList,path+fileName);
        //下载文件
        //String downloadFielName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");
        String fileName2 = URLEncoder.encode(reportVOList.get(0).getTitle() + ".xls","UTF-8");

        HttpHeaders httpHeaders = new HttpHeaders();
        //文件名乱码
        httpHeaders.setContentDispositionFormData("attachment",fileName2);

        //通知浏览器打开文件的方式
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<>(FileUtils.readFileToByteArray(new File(path+fileName)),httpHeaders,HttpStatus.CREATED);
    }


    public void excel(List<StudentReportVO> reportData, String path){
        String[] title = {"学号", "姓名", "分数"};
        //创建Excel文件
        File file = new File(path);
        try {
            //创建工作簿
            WritableWorkbook workbook = Workbook.createWorkbook(file);
            WritableSheet sheet = workbook.createSheet("sheet1", 0);
            jxl.write.Label label = null;
            //第一行设置列名
            for (int i = 0; i < title.length; i++) {
                label = new jxl.write.Label(i, 0, title[i]);
                sheet.addCell(label);
            }
            //追加数据
            for (int i = 1; i <= reportData.size(); i++) {
                label = new jxl.write.Label(0, i, reportData.get(i - 1).getId());
                sheet.addCell(label);
                label = new jxl.write.Label(1, i, reportData.get(i - 1).getName());
                sheet.addCell(label);
                label = new jxl.write.Label(2, i, reportData.get(i - 1).getPoint()+"");
                sheet.addCell(label);
            }
            //写入数据
            workbook.write();
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




    /**
     * 如果文件夹不存在，那么创建文件夹
     * @param s
     */
    public void checkPath(String s){
        File f=new File(s);
        if(!f.exists()){
            f.mkdirs();
        }
    }

    public void createCharts(String path, StatisticsData data) throws IOException {
        //数据
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("<60%", data.getUnderSixty().size());
        dataset.setValue("60%-80%", data.getSixtyAndEighty().size());
        dataset.setValue("80%-90%", data.getEightyAndNinety().size());
        dataset.setValue(">90", data.getAboveNinety().size());

        //创建 Jfreechart对象
        //1. 标题
        //2. 数据源
        //3. 是否显示图例
        //4. 是否包含提示文本
        //5. 是否生成超链接
        JFreeChart jfreechart = ChartFactory.createPieChart(data.getTitle(),
                dataset, true, true, false);

        PiePlot piePlot = (PiePlot) jfreechart.getPlot();
        //5-------------------------------------------------
        //设置上面的样式,0表示KEY,1表示VALUE,2表示百分之几,DecimalFormat用来显示百分比的格式
        piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}=>{1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));


        //6-------------------------------------------------
        //设置下面方框的样式,0表示KEY,1表示VALUE,2表示百分之几,DecimalFormat用来显示百分比的格式
        piePlot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}=>{1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));

        //8------------------------------------------------
        //定义字体格式
        Font font = new Font("微软雅黑", Font.CENTER_BASELINE, 12);
        //定义图片标题
        TextTitle title = new TextTitle(data.getTitle());
        //设置标题的格式
        title.setFont(font);
        //把标题设置到图片里面
        jfreechart.setTitle(title);

        //9------------------------------------------------
        //设置字体,非常关键不然会出现乱码的,下方的字体
        jfreechart.getLegend().setItemFont(font);
        //Pie图的字体
        piePlot.setLabelFont(font);


        //保存到硬盘上
        ChartUtilities.saveChartAsPNG(new File(path), jfreechart, 500, 500);
    }

}
